﻿using DBMode;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;

namespace CMS.ViewModels
{
    public class UserViewModel:BindableBase
    {
        public UserViewModel(IMethods<Member> methods,IDialogService dialogService)
        {
            this.methods = methods;
            this.dialogService = dialogService;
            Refresh();
        }
        private List<Member> members=new List<Member>();
        private readonly IMethods<Member> methods;
        private readonly IDialogService dialogService;

        public List<Member> Members
        {
			get { return members; }
			set { members = value;RaisePropertyChanged(); }
		}


        public DelegateCommand<object> AddUserCommand { get { return new DelegateCommand<object>(AddUser); } }

        private void AddUser(object obj)
        {
            IDialogParameters dialogParameters = new DialogParameters();
            dialogParameters.Add("addUser",null);
            dialogService.ShowDialog("AddUserView", dialogParameters, callback =>
            {
                if (callback.Result == ButtonResult.OK)
                { Refresh(); }
            });
        }

        public DelegateCommand<object> EditCommand { get { return new DelegateCommand<object>(Edit); } }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Edit(object obj)
        {
            if (obj != null)
            {
                if (obj is Member member)
                {
                    int a =methods.Update(member);
                    if (a != 0) MessageBox.Show("操作成功");
                    else
                        MessageBox.Show("操作失败");
                }
            }
        }

        /// <summary>
        /// 更新members列表
        /// </summary>
        public void Refresh()
        {
            Members = methods.Query();
        }


    }
}
